//前导零函数 
function beforeZero(number) {
  if ((number >= 0)&&(number < 10)) {
     return "0" + number
  } else {
    return number
  }
}

//百度地图的初始化
function mapInit() {
	var map = new BMap.Map("allmap");// 创建Map实例
  var point =  new BMap.Point(116.404, 39.915);//创建一个坐标点
  map.centerAndZoom(point,15);  // 初始化地图,设置中心点坐标和地图级别
  map.setCurrentCity("北京");          // 设置地图显示的城市 此项是必须设置的

  var geolocation = new BMap.Geolocation();
  geolocation.getCurrentPosition(function(r){
    if(this.getStatus() == BMAP_STATUS_SUCCESS){
      var mk = new BMap.Marker(r.point);
      map.addOverlay(mk);
      map.panTo(r.point);
    }       
  },{enableHighAccuracy: true})  
}

//初始化地图并设置一个覆盖物
function mapInit2(option) {
    this.map = new BMap.Map("allmap");
    this.range = option.range;

    if (!!option.point) {
      this.point = new BMap.Point(option.point[0], option.point[1]);
    } else {
      this.point =  new BMap.Point(116.404, 39.915);
    }
    this.map.centerAndZoom(this.point,16);
    this.map.setCurrentCity("北京");

    var _this = this;
    if (!option.point) {
      this.geolocation = new BMap.Geolocation();
      this.geolocation.getCurrentPosition(function(r){
        if(this.getStatus() == BMAP_STATUS_SUCCESS){
          _this.point = new BMap.Marker(r.point);
          _this.addMarker(_this.point, _this.range * 10);
          _this.map.panTo(_this.point.point);
        }       
      },{enableHighAccuracy: true})  
    } else {
      this.addMarker(this.point, this.range * 10);
    }

    // 设置点击事件是否添加标注物
    this.isChangeMarker = false;
    this.map.addEventListener("click", changeMarker);
    function changeMarker(e) {
      if(_this.isChangeMarker) {
        _this.removeMarker();
        _this.point = e.point;
        _this.addMarker(e.point, _this.range * 10);
      }
    }  
}
// 添加覆盖物
mapInit2.prototype.addMarker = function(point,r) {
  this.marker = new BMap.Marker(point);
  this.circle = new BMap.Circle(point, r,{strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); 
  this.map.addOverlay(this.marker);
  // this.marker.enableDragging()
  this.map.addOverlay(this.circle);  
}
//移除覆盖物
mapInit2.prototype.removeMarker = function() {
  this.map.removeOverlay(this.marker);
  this.map.removeOverlay(this.circle);  
}
mapInit2.prototype.changeR = function(r) {
  this.circle.setRadius(r);
}
mapInit2.prototype.changeMarker = function(point,r) {

}

// 初始化一个日期选项
function dateInit(obj) {
    var time = new Date();
    var year = time.getYear() + 1900;
    var month =  beforeZero(time.getMonth() + 1);
    var date =  beforeZero(time.getDate());
    var str = year + "-" + month + "-" + date;
    $(obj).calendar({
        value: [str]
    });
}